package com.mujin.keji.collectionsystem.module.outboundtask.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.BoundTaskDto;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.mujin.keji.collectionsystem.utils.NumberUtils;

import java.util.List;


/**
 * 拜访任务列表适配器
 *
 * @author weishixiong
 * @Time 2018-03-21
 */

public class BoundTaskListAdapter extends RecyclerView.Adapter<BoundTaskListAdapter.ViewHolder> {

    private Activity mContext;
    private List<BoundTaskDto> datas;
    private BoundTaskListener listener;


    /**
     * 点击拜访接口记录回调
     */
    public interface BoundTaskListener {
        /**
         * 跳转到案件信息
         *
         * @param dto
         */
        void toCaseInformation(BoundTaskDto dto,int position);

        /**
         * 跳转到拜访详情
         *
         * @param dto
         */
        void toBoundDetail(BoundTaskDto dto,int position);
    }

    public BoundTaskListAdapter(Activity context, List<BoundTaskDto> datas, BoundTaskListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_baseoutboundtask, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvName.setText(datas.get(position).getVisitName());
        holder.tvBeforDay.setText(datas.get(position).getLastCollectionDate());
        holder.tvAccount.setText("最新欠款额：" + NumberUtils.twoDecimalPlaces(datas.get(position).getLatestDebtMoney()));
        if(EmptyUtils.isNotEmpty(datas.get(position).getOverdueDay())){
            holder.tvDays.setText("逾期：" + datas.get(position).getOverdueDay() + "天");
        }
        else{
            holder.tvDays.setText("逾期：-天");
        }
        holder.tvAdress.setText(datas.get(position).getAddress());

//        holder.tvAdress.setText(datas.get(position).getVisitAddress()==null?"-":datas.get(position).getVisitAddress());
//        holder.tvIdentity.setText(datas.get(position).getVisitRelation()==null?"-":datas.get(position).getVisitRelation());
        holder.tvAllocateTime.setText(datas.get(position).getAllotTime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.toCaseInformation(datas.get(position),position);
            }
        });
        holder.tvVisit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.toBoundDetail(datas.get(position),position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private TextView tvBeforDay;
        private TextView tvAccount;
        private TextView tvDays;
        private TextView tvAdress;
        private TextView tvIdentity;
        private TextView tvAllocateTime;
        private TextView tvVisit;
        private View vLine;
        private View itemView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvBeforDay = (TextView) itemView.findViewById(R.id.tv_befor_day);
            tvAccount = (TextView) itemView.findViewById(R.id.tv_account);
            tvDays = (TextView) itemView.findViewById(R.id.tv_days);
            tvAdress = (TextView) itemView.findViewById(R.id.tv_adress);
            tvIdentity = (TextView) itemView.findViewById(R.id.tv_identity);
            tvAllocateTime = (TextView) itemView.findViewById(R.id.tv_allocate_time);
            tvVisit = (TextView) itemView.findViewById(R.id.tv_visit);
            vLine = (View) itemView.findViewById(R.id.v_line);
        }

    }
}
